package com;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DrillBoxGeneratorII {

    public static void main(String[] args) throws IOException {
        String jsonInput = "{\n" +
                "  \"macCode\": \"E001\",\n" +
                "  \"transCode\": \"dgas-ihewotru8r-t7345rw\",\n" +
                "  \"axles\": 10,\n" +
                "  \"drillArray\": [\n" +
                "    {\n" +
                "      \"tools\": \"T01\",\n" +
                "      \"drillSize\": \"3175\",\n" +
                "      \"drillCount\": 3,\n" +
                "      \"holeType\": \"1\",\n" +
                "      \"drillGrindCount\": 2\n" +
                "    },\n" +
                "    {\n" +
                "      \"tools\": \"T02\",\n" +
                "      \"drillSize\": \"0200\",\n" +
                "      \"drillCount\": 118,\n" +
                "      \"holeType\": \"1\",\n" +
                "      \"drillGrindCount\": 1\n" +
                "    },\n" +
                "    {\n" +
                "      \"tools\": \"T03\",\n" +
                "      \"drillSize\": \"0225\",\n" +
                "      \"drillCount\": 80,\n" +
                "      \"holeType\": \"1\",\n" +
                "      \"drillGrindCount\": 0\n" +
                "    },\n" +
                "    {\n" +
                "      \"tools\": \"T04\",\n" +
                "      \"drillSize\": \"0250\",\n" +
                "      \"drillCount\": 60,\n" +
                "      \"holeType\": \"1\",\n" +
                "      \"drillGrindCount\": 2\n" +
                "    },\n" +
                "    {\n" +
                "      \"tools\": \"T05\",\n" +
                "      \"drillSize\": \"0275\",\n" +
                "      \"drillCount\": 28,\n" +
                "      \"holeType\": \"1\",\n" +
                "      \"drillGrindCount\": 2\n" +
                "    },\n" +
                "    {\n" +
                "      \"tools\": \"T06\",\n" +
                "      \"drillSize\": \"0300\",\n" +
                "      \"drillCount\": 22,\n" +
                "      \"holeType\": \"1\",\n" +
                "      \"drillGrindCount\": 4\n" +
                "    },\n" +
                "    {\n" +
                "      \"tools\": \"T07\",\n" +
                "      \"drillSize\": \"0350\",\n" +
                "      \"drillCount\": 16,\n" +
                "      \"holeType\": \"1\",\n" +
                "      \"drillGrindCount\": 2\n" +
                "    },\n" +
                "    {\n" +
                "      \"tools\": \"T08\",\n" +
                "      \"drillSize\": \"0400\",\n" +
                "      \"drillCount\": 8,\n" +
                "      \"holeType\": \"1\",\n" +
                "      \"drillGrindCount\": 3\n" +
                "    },\n" +
                "    {\n" +
                "      \"tools\": \"T09\",\n" +
                "      \"drillSize\": \"0450\",\n" +
                "      \"drillCount\": 6,\n" +
                "      \"holeType\": \"2\",\n" +
                "      \"drillGrindCount\": 2\n" +
                "    },\n" +
                "    {\n" +
                "      \"tools\": \"T10\",\n" +
                "      \"drillSize\": \"0450\",\n" +
                "      \"drillCount\": 4,\n" +
                "      \"holeType\": \"2\",\n" +
                "      \"drillGrindCount\": 2\n" +
                "    },\n" +
                "    {\n" +
                "      \"tools\": \"T11\",\n" +
                "      \"drillSize\": \"0500\",\n" +
                "      \"drillCount\": 3,\n" +
                "      \"holeType\": \"2\",\n" +
                "      \"drillGrindCount\": 2\n" +
                "    },\n" +
                "    {\n" +
                "      \"tools\": \"T12\",\n" +
                "      \"drillSize\": \"0650\",\n" +
                "      \"drillCount\": 4,\n" +
                "      \"holeType\": \"1\",\n" +
                "      \"drillGrindCount\": 1\n" +
                "    },\n" +
                "    {\n" +
                "      \"tools\": \"T13\",\n" +
                "      \"drillSize\": \"5000\",\n" +
                "      \"drillCount\": 1,\n" +
                "      \"holeType\": \"1\",\n" +
                "      \"drillGrindCount\": 2\n" +
                "    },\n" +
                "    {\n" +
                "      \"tools\": \"T14\",\n" +
                "      \"drillSize\": \"6000\",\n" +
                "      \"drillCount\": 2,\n" +
                "      \"holeType\": \"1\",\n" +
                "      \"drillGrindCount\": 1\n" +
                "    }\n" +
                "  ],\n" +
                "  \"Timestamp\": \"2024-12-10 22:31:09\"\n" +
                "}";

        ObjectMapper mapper = new ObjectMapper();
        JsonNode root = mapper.readTree(jsonInput);
        JsonNode drillArray = root.get("drillArray");

        // 主刀盒处理
        List<String[][]> mainBoxes = generateMainBoxes(drillArray);
        // 备用刀盒处理
        List<String[][]> backupBoxes = generateBackupBoxes(drillArray);

        // 打印结果
        System.out.println("主刀盒数量: " + mainBoxes.size());
        for (int i = 0; i < mainBoxes.size(); i++) {
            System.out.println("主刀盒 " + (i + 1) + ":");
            printBox(mainBoxes.get(i));
        }

        System.out.println("备用刀盒数量: " + backupBoxes.size());
        for (int i = 0; i < backupBoxes.size(); i++) {
            System.out.println("备用刀盒 " + (i + 1) + ":");
            printBox(backupBoxes.get(i));
        }
    }

    private static List<String[][]> generateMainBoxes(JsonNode drillArray) {
        List<String[][]> mainBoxes = new ArrayList<>();
        List<String> mainTools = new ArrayList<>();

        // 收集主刀盒中的刀具
        for (JsonNode tool : drillArray) {
            mainTools.add(tool.get("tools").asText());
        }

        // 计算需要多少个主刀盒
        int mainBoxCount = (int) Math.ceil(mainTools.size() / 50.0);
        for (int i = 0; i < mainBoxCount; i++) {
            String[][] box = new String[10][5]; // 10行5列
            int startIndex = i * 50;
            int endIndex = Math.min(startIndex + 50, mainTools.size());

            for (int j = 0; j < 5; j++) { // 5列
                for (int k = 0; k < 10; k++) { // 10行
                    int toolIndex = startIndex + j * 10 + k;
                    if (toolIndex < endIndex) {
                        box[k][j] = mainTools.get(toolIndex);
                    } else {
                        box[k][j] = "";
                    }
                }
            }
            mainBoxes.add(box);
        }

        return mainBoxes;
    }

    private static List<String[][]> generateBackupBoxes(JsonNode drillArray) {
        List<String[][]> backupBoxes = new ArrayList<>();
        List<ToolInfo> toolInfos = new ArrayList<>();

        // 收集每种刀具的剩余数量
        for (JsonNode tool : drillArray) {
            int count = tool.get("drillCount").asInt();
            String toolId = tool.get("tools").asText();
            toolInfos.add(new ToolInfo(toolId, count - 1)); // 减去主刀盒中的一支
        }

        // 按刀具剩余数量排序（可选）
//        toolInfos.sort((a, b) -> b.count - a.count);

        List<String> backupTools = new ArrayList<>();
        for (ToolInfo toolInfo : toolInfos) {
            for (int i = 0; i < toolInfo.count; i++) {
                backupTools.add(toolInfo.toolId);
            }
        }

        // 计算需要多少个备用刀盒
        int backupBoxCount = (int) Math.ceil(backupTools.size() / 50.0);
        for (int i = 0; i < backupBoxCount; i++) {
            String[][] box = new String[10][5]; // 10行5列
            int startIndex = i * 50;
            int endIndex = Math.min(startIndex + 50, backupTools.size());

            int currentToolIndex = startIndex;
            for (int j = 0; j < 5; j++) { // 5列
                if (currentToolIndex >= endIndex) break;

                String currentTool = backupTools.get(currentToolIndex);
                int countInColumn = 0;

                for (int k = 0; k < 10; k++) { // 10行
                    if (currentToolIndex + k < endIndex &&
                            backupTools.get(currentToolIndex + k).equals(currentTool)) {
                        box[k][j] = currentTool;
                        countInColumn++;
                    } else {
                        box[k][j] = "";
                    }
                }

                currentToolIndex += countInColumn;
            }

            backupBoxes.add(box);
        }

        return backupBoxes;
    }

    private static void printBox(String[][] box) {
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print(box[i][j] + "\t");
            }
            System.out.println();
        }
        System.out.println();
    }

    private static class ToolInfo {
        String toolId;
        int count;

        ToolInfo(String toolId, int count) {
            this.toolId = toolId;
            this.count = count;
        }
    }
}
